<?php
namespace common\bonus;
use common\models\Member;


use common\bonus\Recommend;
use common\bonus\TeamTotal;
use common\models\TradeModel;
use common\utils\CommonFun;
/*
	分红模式基础类
*/
class BaseBonus
{

	/*
		分红入口
		* money  		分红基数金额
		* source_id 	来源id/外部订单号
		* member_id     触发人id/下单、投资的会员id
		* trigger_type  触发类型 1投资报单触发 2订单触发
		* mch_id        商户id
		* desc   		描述
		* isteam        是否需要获取团队信息 默认是需要
	*/
	public static function bonusRun($params)
	{

		//获取触发事件
		$data = TradeModel::getTriggerEvent($params['trigger_type']);

		if(!$data) return true;

		$field = ['member_id','invite_id','grade','out_mid','m_grade','user_type'];
		$params['member'] = Member::getMemberById($params['member_id'],$field);


		$isteam = isset($params['isteam'])?$params['isteam']:true;
		if($params['member_id'] && $isteam)
		{
		    $params['uper']= self::getAllUper($params['member_id']);
		}


		foreach($data as $val)
		{
		    $modelDir  = 'common\\bonus\\'.$val['model_code'];
		    $model = new $modelDir();
		    $res = $model::bonusAction($params);
		    if(!$res['state']) CommonFun::writeLog('【'.$val['model_code'].'】'.$res['msg'],'bonus');
		}
		return true;
	}





    //找出所有上级  包括自己
    private static function getAllUper($inviteId,&$list=array())
    {
        if($inviteId>0)
        {
            $member = Member::getMemberById($inviteId,['member_id','invite_id','grade','out_mid','m_grade']);
            if($member)
            {
                $list[] = $member;
                self::getAllUper($member['invite_id'],$list);
            }
        }
        return $list;
    }
}